<?php
// 首页显示信息以及基本配置
namespace NewAdmin\Controller;
use Think\Controller;
class PortalController extends CommonController {
    function _initialize() {
        parent::_initialize();
       
    }
  	function article_list(){
  		$article_posts=M('article_posts');
  		$count      = $article_posts->where('post_type=0')->count();
	    $this->assign('count',$count);
	    
	    $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
	    $Page->setConfig('next','下一页');
	    $Page->setConfig('prev','上一页');
	    $show       = $Page->show();// 分页显示输出
  		$article_list=$article_posts
  				->field('yt_article_posts.post_title,yt_article_posts.post_date,yt_article_posts.id,yt_article_posts.post_hits,yt_article_posts.post_status,yt_article_terms.name')
  				->join('yt_article_terms ON yt_article_terms.term_id = yt_article_posts.trem_id')
  				->where('yt_article_posts.post_type=0')
  				->limit($Page->firstRow.','.$Page->listRows)
  				->select();
  		$this->assign('page',$show);// 赋值分页输出		
  		$this->assign('article_list',$article_list);
  		$this->assign('title','文章列表');
  		$this->display();
  	}
  	
  	function page_list(){
  		$article_posts=M('article_posts');
  		$count      = $article_posts->where('post_type=1')->count();
	    $this->assign('count',$count);
	    
	    $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
	    $Page->setConfig('next','下一页');
	    $Page->setConfig('prev','上一页');
	    $show       = $Page->show();// 分页显示输出
  		$article_list=$article_posts
  				->where('post_type=1')
  				->limit($Page->firstRow.','.$Page->listRows)
  				->select();
  		$this->assign('page',$show);// 赋值分页输出		
  		$this->assign('article_list',$article_list);
  		$this->assign('title','页面列表');
  		$this->display();
  	}
  	
  	function addpage(){
  		if($_POST){
  			$data=I('post.');
  			$data['post_type']=1;
  			$data['post_status']=1;
  			$data['post_date']=date('Y-m-d H:i:s',time());
  			$article_posts=M('article_posts');
  			$rs=$article_posts->add($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}else{
  			$term=M('article_terms');
	  		$list=$term->where('parent=0')->order('listorder asc')->select();
	  		foreach($list as $key=>$value){
	  			$child_list=$term->where('parent='.$value['term_id'])->select();
	  			$list[$key]['child']=$child_list;
	  		}
	  		$this->assign('list',$list);
	  		$this->assign('title','添加页面');
	  		$this->display();
  		}
  		
  	}
  	
  	function addArticle(){
  		if($_POST){
  			$data=I('post.');
  			$data['post_type']=0;
  			$data['post_status']=1;
  			$data['post_date']=date('Y-m-d H:i:s',time());
  			$article_posts=M('article_posts');
  			$rs=$article_posts->add($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}else{
  			$term=M('article_terms');
	  		$list=$term->where('parent=0')->order('listorder asc')->select();
	  		foreach($list as $key=>$value){
	  			$child_list=$term->where('parent='.$value['term_id'])->select();
	  			$list[$key]['child']=$child_list;
	  		}
	  		$this->assign('list',$list);
	  		$this->assign('title','添加文章');
	  		$this->display();
  		}
  		
  	}
  	
  	function updatepage(){
  		$article_posts=M('article_posts');
  		if($_POST){
  			$data=I('post.');
  			$data['post_type']=1;
  			$data['post_status']=1;
  			$article_posts=M('article_posts');
  			$rs=$article_posts->save($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}else{
  			
	  		$article=$article_posts->find(I('get.id'));
	  		$this->assign('article',$article);
	  		$this->assign('title','文章列表');
	  		$this->display();
  		}
  	}
  	
  	function updateArticle(){
  		$article_posts=M('article_posts');
  		if($_POST){
  			$data=I('post.');
  			$data['post_type']=0;
  			$data['post_status']=1;
  			$article_posts=M('article_posts');
  			$rs=$article_posts->save($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}else{
  			$term=M('article_terms');
	  		$list=$term->where('parent=0')->order('listorder asc')->select();
	  		foreach($list as $key=>$value){
	  			$child_list=$term->where('parent='.$value['term_id'])->select();
	  			$list[$key]['child']=$child_list;
	  		}
	  		$article=$article_posts->find(I('get.id'));
	  		$this->assign('list',$list);
	  		$this->assign('article',$article);
	  		$this->assign('title','文章列表');
	  		$this->display();
  		}
  	}
  	
  	function trem_list(){
  		$term=M('article_terms');
  		$list=$term->where('parent=0')->order('listorder asc')->select();
  		foreach($list as $key=>$value){
  			$child_list=$term->where('parent='.$value['term_id'])->select();
  			$list[$key]['child']=$child_list;
  		}
  		$this->assign('list',$list);
  		$this->assign('title','分类列表');
  		$this->display();
  	}
  	
  	function trem_list_add(){
  		$term=M('article_terms');
  		$this->assign('title','添加分类');
  		if($_POST){
  			$p_data=I('post.');
  			$data['name']=$p_data['name'];
  			$data['parent']=$p_data['pid'];
  			$data['seo_title']=$p_data['seo_title'];
  			$data['seo_keywords']=$p_data['seo_keywords'];
  			$data['seo_description']=$p_data['seo_description'];
  			$data['listorder']=$p_data['sort'];
  			$rs=$term->add($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}else{
  			$list=$term->where('parent=0')->select();
  			$this->assign('list',$list);
  			$this->display();
  		}
  	}
  	
  		
  	function trem_list_edit(){
  		$term=M('article_terms');
  		$this->assign('title','编辑分类');
  		if($_POST){
  			$p_data=I('post.');
  			$data['name']=$p_data['name'];
  			$data['parent']=$p_data['pid'];
  			$data['seo_title']=$p_data['seo_title'];
  			$data['seo_keywords']=$p_data['seo_keywords'];
  			$data['seo_description']=$p_data['seo_description'];
  			$data['listorder']=$p_data['sort'];
  			$data['term_id']=$p_data['term_id'];
  			$rs=$term->save($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}else{
  			$lists=$term->where('parent=0 and term_id!='.I('get.id'))->select();
  			$this->assign('lists',$lists);
  			$list=$term->find($_GET['id']);
  			$this->assign('list',$list);
  			$this->display();
  		}
  	}
  	
  	function modifyField(){
  		if($_POST){
  			$data['listorder']=I('post.sort');
  			$data['term_id']=I('post.fieldid');
  			$term=M('article_terms');
  			$rs=$term->save($data);
  			if($rs){
  				$info['code']=1;
  				$this->ajaxReturn($info);
  			}else{
  				$info['code']=0;
  				$this->ajaxReturn($info);
  			}
  		}
  	}
  	
  	function del_trem(){
  		if($_POST){
  			$data['term_id']=I('post.class_id');
  			
  			$term=M('article_terms');
  			$lists=$term->where("parent=".I('post.class_id'))->select();
  			if($lists){
  				$info['code']=2;
	  			$this->ajaxReturn($info);
  			}else{
  				$rs=$term->delete(I('post.class_id'));
  				if($rs){
	  				$info['code']=1;
	  				$this->ajaxReturn($info);
	  			}else{
	  				$info['code']=0;
	  				$this->ajaxReturn($info);
	  			}
  			}
  			
  			
  		}
  	}
    function del_page(){
        if($_POST){
            $id=I('post.class_id');
            $posts=M('article_posts');
            $rs=$posts->delete($id);
            if($rs){
                $info['code']=1;
                $this->ajaxReturn($info);
            }else{
                $info['code']=0;
                $this->ajaxReturn($info);
            }
        }
    }
  	
  	
}